package daoImpl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import dao.ShangpinDao;
import entlty.Louceng;
import entlty.Shangpin;
import utils.BaseDao;

public class ShangpinDaoImpl extends BaseDao implements ShangpinDao{

	@Override
	public List<Shangpin> getAll() {
		List<Shangpin> list=new ArrayList<Shangpin>();
		String sql="select * from shangpin s join louceng l on s.l_id=l.l_id";
		ResultSet rs=super.exeQuery(sql);
		try {
			while(rs.next()){
				Shangpin s=new Shangpin(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getInt(5),rs.getInt(6));
				
				Louceng l=new Louceng(rs.getInt("l_id"),rs.getString("l_name"));
				s.setLouceng(l);
				list.add(s);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return list;
	}

	@Override
	public List<Shangpin> getAll(Shangpin s) {

		String sql="select * from shangpin s join louceng l on s.l_id=l.l_id";
		if(s!=null){
			if(s.getSname()!=null){
				sql+=" and s.sname like '%"+s.getSname()+"%'";
			}
		}
		
		
		
		
		List<Shangpin> list=new ArrayList<Shangpin>();
		ResultSet rs=super.exeQuery(sql);
		try {
			while(rs.next()){
				Shangpin ss=new Shangpin(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getInt(5),rs.getInt(6));
				
				Louceng l=new Louceng(rs.getInt("l_id"),rs.getString("l_name"));
				ss.setLouceng(l);
				list.add(ss);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return list;
	}

	@Override
	public Shangpin Byid(int s_id) {
		Shangpin sp=null;
		String sql="select * from shangpin where s_id=?";
		ResultSet rs=super.exeQuery(sql, s_id);
		try {
			while(rs.next()){
				sp=new Shangpin(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getInt(5),rs.getInt(6));
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return sp;
	}



}
